<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Store\Controller;
use Store\Controller\StoreController;

class OrderController extends StoreController {
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys','order');// 数据集
    }
    
    //put your code here
    public function index() {
        $order_sn = I('order_sn');
        $buyer_name = I('buyer_name');
        $query_start_time = strtotime(I('query_start_time'));
        $query_end_time = strtotime(I('query_end_time'));

        if(!empty($order_sn)){
            $where['oi.order_sn'] = $order_sn;
        }
        if (!empty($buyer_name)) {
            $where['os.reciver_name'] = array('like', '%' . $buyer_name . '%');
        }

        if (!empty($query_end_time) && !empty($query_end_time)) {
            $where['oi.add_time'] = array('between', array($query_start_time, $query_end_time));
        }
        
        $where['oi.store_id'] = session('store_id');
        $Order = M("Order");
        $count = $Order->alias('oi')->join('LEFT JOIN __ORDER_COMMON__ os ON oi.order_id = os.order_id')->where($where)->count();
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $res = $Order->alias('oi')->join('LEFT JOIN __ORDER_COMMON__ os ON oi.order_id = os.order_id')->where($where)->order('oi.order_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('pages', $show);
        $this->assign('res', $res);
        $this->assign('action', 'index');
        $this->display();
    }

    // 编辑
    public function edit(){
        if(IS_POST) {
            $id['order_id'] = I('get.order_id');
            $datas = I('post.');
            $data['shipping_express_id'] = I('post.shipping_express_id');

            M('Order')->where($id)->data($datas)->save();
            M('order_common')->where($id)->data($data)->save();
            $this->success('操作成功',U('index'));
        }else{
            $id['order_id'] = I('order_id');
            $order = D('Order')->relation('OrderCommon')->where($id)->find();//订单信息
            $order['OrderCommon']['reciver_info'] = unserialize($order['OrderCommon']['reciver_info']);

            $data['id'] = array('in',array($order['OrderCommon']['reciver_info']['province_id'],$order['OrderCommon']['reciver_info']['city_id'],$order['OrderCommon']['reciver_info']['area_id']));
            $area = M('area')->where($data)->select(); //获得地区
            $orderGoods = M('orderGoods')->where($id)->select();//订单商品信息
            $express = M('express')->select();//物流公司信息
            $this->assign('express',$express);
            $this->assign('area',$area);
            $this->assign('orderGoods',$orderGoods);
            $this->assign('res',$order);

            $this->display();
        }
    }

    //订单查询
    public function orderInquiry(){
        if(IS_POST){


            $where['tdd_order.store_id'] = session('store_auth.store_id');
            $p = I('post.');
            $p['order_sn'] ? $where['tdd_order.order_sn'] = $p['order_sn'] : false; //订单号
            $p['pay_sn'] ? $where['tdd_order.pay_sn'] = $p['pay_sn'] : false; //支付单号
            $p['store_name'] ? $where['tdd_order.store_name'] = $p['store_name'] : false; //卖家店铺
            $p['buyer_email'] ? $where['tdd_order.buyer_email'] = $p['buyer_email'] : false; //电子邮箱
            $p['shipping_code'] ? $where['tdd_order.shipping_code'] = $p['shipping_code'] : false; //物流单号
            $p['payment_code'] ? $where['tdd_order.payment_code'] = $p['payment_code'] : false; //付款方式
            $p['order_state'] ? $where['tdd_order.order_state'] = $p['order_state'] : false; //订单状态
            if($p['query_start_time'] && $p['query_end_time']){
                $where['tdd_order.add_time'] = array(array('EGT',$p['query_start_time']),array('ELT',$p['query_end_time']));//两个时间同时存在
            }else{
                $p['query_start_time'] ? $where['tdd_order.add_time'] = array('EGT',$p['query_start_time']) : false; //只有开始时间
                $p['query_end_time'] ? $where['tdd_order.add_time'] = array('ELT',$p['query_end_time']) : false; //只有结束时间
            }

            $p['buyer_name'] ? $where['tdd_order_common.reciver_name'] = $p['buyer_name'] : false; //收货人
            $p['user_provinceid'] ? $where['tdd_order_common.reciver_province_id'] = $p['user_provinceid'] : false; //省
            $p['user_cityid'] ? $where['tdd_order_common.reciver_reciver_city_id'] = $p['user_cityid'] : false; //市
            $p['user_areaid'] ? $where['tdd_order_common.reciver_areaid_id'] = $p['user_areaid'] : false; //县（区）
            $p['phone'] ? $where['tdd_order_common.reciver_info'] = array('like','%'.$p['phone'].'%') : false; //手机号

            $Order = M("order");
            $count = $Order->join('LEFT JOIN tdd_order_common ON tdd_order.order_id = tdd_order_common.order_id')->where($where)->count();
            $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)

            $show = $Page->show(); // 分页显示输出
            $arr = $Order->join('LEFT JOIN tdd_order_common ON tdd_order.order_id = tdd_order_common.order_id')->where($where)->order('tdd_order.order_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
            $this->assign('pages',$show);
            $this->assign('res',$arr);
            $this->display('index');die;
        }

        $this->display();
    }
    
}
